package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Strings;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.Menu;
import com.jrtc.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 菜单表(Menu)表控制层
 *
 * @author makejava
 * @since 2022-09-06 16:46:29
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Menu selectOne(Integer id) {
        return this.menuService.queryById(id);
    }

    //获取所有菜单
    @RequestMapping("getMenuList")
    public ResultDTO getMenuList()
    {
//        IPage<Menu> ipage = menuService.queryMenuList(menu);
        List<Menu> lstMenu = menuService.getMenuList();
        return ResultDTOBuilder.success(lstMenu);
    }

    @RequestMapping("insertMenu")
    public ResultDTO insertMenu(@RequestBody Menu menu)
    {
//        menu.setId(UUID.randomUUID().toString());
        Menu tmpMenu = menuService.insert(menu);
        if (tmpMenu != null)
        {
            return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getMenuById")
    public ResultDTO getMenuById(@RequestBody Map<String, String> map)
    {
        String menuId = map.get("menuId");
        Menu menu = menuService.queryById(Integer.valueOf(menuId));
        if (Strings.isNullOrEmpty(menuId) || menu==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(menu);
    }

    @RequestMapping("updateMenu")
    public ResultDTO updateMenu(@RequestBody Menu menu)
    {
        if (menu==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (menuService.update(menu)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }

    @RequestMapping("deleteMenu")
    public ResultDTO deleteMenu(@RequestBody Map<String,String> map)
    {
        String menuId = map.get("menuId");
        if (!Strings.isNullOrEmpty(menuId))
        {
            if (menuService.deleteById(Integer.valueOf(menuId)))
            {
                return ResultDTOBuilder.success("删除成功！");
            }
            return ResultDTOBuilder.failureMsg("删除失败！");
        }
        return ResultDTOBuilder.failureMsg("编号不正确！");
    }
}